﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MapEditor.GUI
{
    class NewMapButton : Button
    {
        //Declare instance variable
        public bool clicked = false;

        //Constructor
        public NewMapButton(Texture2D texture, Vector2 position) : base(texture, position)
        {

        }

        public override void Update()
        {
            clicked = base.Clicked;
            base.Update();
        }

        public override void Effect()
        {
            Game1.state = State.FREEZE;

            //Set the variables from the form
            //Create and display the form
            Forms.NewMapForm newMap = new Forms.NewMapForm();
            newMap.ShowDialog();

            if (newMap.DialogResult == System.Windows.Forms.DialogResult.OK)
            {
                Game1.Front1LayerType = newMap.FL1TileType;
                Game1.FrontLayer1Height = newMap.FL1Height;
                Game1.FrontLayer1Width = newMap.FL1Width;
                Game1.Front1TileHeight = newMap.FL1TileHeight;
                Game1.Front1TileWidth = newMap.FL1TileWidth;

                Game1.Front2LayerType = newMap.FL2TileType;
                Game1.FrontLayer2Height = newMap.FL2Height;
                Game1.FrontLayer2Width = newMap.FL2Width;
                Game1.Front2TileHeight = newMap.FL2TileHeight;
                Game1.Front2TileWidth = newMap.FL2TileWidth;

                Game1.Front3LayerType = newMap.FL3TileType;
                Game1.FrontLayer3Height = newMap.FL3Height;
                Game1.FrontLayer3Width = newMap.FL3Width;
                Game1.Front3TileHeight = newMap.FL3TileHeight;
                Game1.Front3TileWidth = newMap.FL3TileWidth;

                Game1.EnemyLayerType = newMap.EnemyTileType;
                Game1.EnemyLayerHeight = newMap.EnemyHeight;
                Game1.EnemyLayerWidth = newMap.EnemyWidth;
                Game1.EnemyTileHeight = newMap.EnemyTileHeight;
                Game1.EnemyTileWidth = newMap.EnemyTileWidth;

                Game1.PlayerLayerType = newMap.PlayerTileType;
                Game1.PlayerLayerHeight = newMap.PlayerHeight;
                Game1.PlayerLayerWidth = newMap.PlayerWidth;
                Game1.PlayerTileHeight = newMap.PlayerTileHeight;
                Game1.PlayerTileWidth = newMap.PlayerTileWidth;

                Game1.ScreenLayerType = newMap.ScreenTileType;
                Game1.ScreenLayerHeight = newMap.ScreenHeight;
                Game1.ScreenLayerWidth = newMap.ScreenWidth;
                Game1.ScreenTileHeight = newMap.ScreenTileHeight;
                Game1.ScreenTileWidth = newMap.ScreenTileWidth;

                Game1.Back1LayerType = newMap.BG1TileType;
                Game1.BackgroundLayer1Height = newMap.BG1Height;
                Game1.BackgroundLayer1Width = newMap.BG1Width;
                Game1.Back1TileHeight = newMap.BG1TileHeight;
                Game1.Back1TileWidth = newMap.BG1TileWidth;

                Game1.Back2LayerType = newMap.BG2TileType;
                Game1.BackgroundLayer2Height = newMap.BG2Height;
                Game1.BackgroundLayer2Width = newMap.BG2Width;
                Game1.Back2TileHeight = newMap.BG2TileHeight;
                Game1.Back2TileWidth = newMap.BG2TileWidth;

                Game1.Back3LayerType = newMap.BG3TileType;
                Game1.BackgroundLayer3Height = newMap.BG3Height;
                Game1.BackgroundLayer3Width = newMap.BG3Width;
                Game1.Back3TileHeight = newMap.BG3TileHeight;
                Game1.Back3TileWidth = newMap.BG3TileWidth;

                //Game1.tileWidth = newMap.tileWidth;
                Game1.mapName = newMap.mapName;

                Game1.selectedTileNo = 0;
                Game1.drawOffset = Microsoft.Xna.Framework.Vector2.Zero;

                //Initialize the map
                Game1.map = new MapEditor.Map_Classes.Map(Game1.FrontLayer1Height, Game1.FrontLayer1Width, Game1.Front1TileHeight, Game1.Front1TileWidth, Game1.Front1LayerType, Game1.FrontLayer2Height, Game1.FrontLayer2Width, Game1.Front2TileHeight, Game1.Front2TileWidth, Game1.Front2LayerType, Game1.FrontLayer3Height, Game1.FrontLayer3Width, Game1.Front3TileHeight, Game1.Front3TileWidth, Game1.Front3LayerType,
                Game1.ScreenLayerHeight, Game1.ScreenLayerWidth, Game1.ScreenTileHeight, Game1.ScreenTileWidth, Game1.ScreenLayerType, Game1.PlayerLayerHeight, Game1.PlayerLayerWidth, Game1.PlayerTileHeight, Game1.PlayerTileWidth, Game1.PlayerLayerType, Game1.EnemyLayerHeight, Game1.EnemyLayerWidth, Game1.EnemyTileHeight, Game1.EnemyTileWidth, Game1.EnemyLayerType, Game1.BackgroundLayer1Height,
                Game1.BackgroundLayer1Width, Game1.Back1TileHeight, Game1.Back1TileWidth, Game1.Back1LayerType, Game1.BackgroundLayer2Height, Game1.BackgroundLayer2Width, Game1.Back2TileHeight, Game1.Back2TileWidth, Game1.Back2LayerType, Game1.BackgroundLayer3Height, Game1.BackgroundLayer3Width, Game1.Back3TileHeight, Game1.Back3TileWidth, Game1.Back3LayerType);

                if (Game1.Front1TileSheet != null && Game1.Front2TileSheet != null && Game1.Front3TileSheet != null && Game1.EnemyTileSheet != null && Game1.PlayerTileSheet != null && Game1.ScreenTileSheet != null && Game1.Back1TileSheet != null && Game1.Back2TileSheet != null && Game1.Back3TileSheet != null)
                {
                    Game1.map.LoadTileSet(Game1.Front1TileSheet, Game1.Front1TileWidth, Game1.Front1TileHeight);
                    Game1.map.LoadTileSet(Game1.Front2TileSheet, Game1.Front2TileWidth, Game1.Front2TileHeight);
                    Game1.map.LoadTileSet(Game1.Front3TileSheet, Game1.Front3TileWidth, Game1.Front3TileHeight);
                    Game1.map.LoadTileSet(Game1.PlayerTileSheet, Game1.PlayerTileWidth, Game1.PlayerTileHeight);
                    Game1.map.LoadTileSet(Game1.EnemyTileSheet, Game1.EnemyTileWidth, Game1.EnemyTileHeight);
                    Game1.map.LoadTileSet(Game1.ScreenTileSheet, Game1.ScreenTileWidth, Game1.ScreenTileHeight);
                    Game1.map.LoadTileSet(Game1.Back1TileSheet, Game1.Back1TileWidth, Game1.Back1TileHeight);
                    Game1.map.LoadTileSet(Game1.Back2TileSheet, Game1.Back2TileWidth, Game1.Back2TileHeight);
                    Game1.map.LoadTileSet(Game1.Back3TileSheet, Game1.Back3TileWidth, Game1.Back3TileHeight);
                }

                base.prevClicked = false;
            }
            else
            {
                base.prevClicked = false;
            }

            //Unfreeze the game
            Game1.state = State.PLAY;

            base.Effect();
        }
    }
}
